<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div>
  <mat-card appearance="outlined" class="settings-card">
    <mat-card-header>
      <mat-card-title>
        <span class="mat-headline-5" translate>admin.2fa.2fa</span>
      </mat-card-title>
      <span class="flex-1"></span>
      <div tb-help="twoFactorAuthentication"></div>
    </mat-card-header>
    <mat-progress-bar color="warn" mode="indeterminate" *ngIf="isLoading$ | async">
    </mat-progress-bar>
    <div style="height: 4px;" *ngIf="!(isLoading$ | async)"></div>
    <mat-card-content>
      <form [formGroup]="twoFaFormGroup" (ngSubmit)="save()">
        <fieldset [disabled]="isLoading$ | async">
          <div>
            <fieldset class="fields-group" formArrayName="providers">
              <legend class="group-title" translate>admin.2fa.available-providers</legend>
              <ng-container *ngFor="let provider of providersForm.controls; let i = index; let $last = last; trackBy: trackByElement">
                <mat-expansion-panel class="provider" [formGroupName]="i">
                  <mat-expansion-panel-header>
                    <mat-panel-title class="flex items-center justify-start">
                      <mat-slide-toggle
                        (mousedown)="toggleExtensionPanel($event, i, provider.get('enable').value)"
                        formControlName="enable">
                        {{ twoFactorAuthProvidersData.get(provider.value.providerType).name | translate }}
                      </mat-slide-toggle>
                    </mat-panel-title>
                  </mat-expansion-panel-header>

                  <ng-template matExpansionPanelContent>
                    <ng-container [ngSwitch]="provider.get('providerType').value">
                      <ng-container *ngSwitchCase="twoFactorAuthProviderType.TOTP">
                        <mat-form-field class="mat-block flex-1">
                          <mat-label translate>admin.2fa.issuer-name</mat-label>
                          <input matInput formControlName="issuerName" required>
                          <mat-error *ngIf="provider.get('issuerName').hasError('required') ||
                                            provider.get('issuerName').hasError('pattern')">
                            {{ "admin.2fa.issuer-name-required" | translate }}
                          </mat-error>
                        </mat-form-field>
                      </ng-container>
                      <div *ngSwitchCase="twoFactorAuthProviderType.SMS"
                           class="flex flex-row xs:flex-col gt-xs:gap-2">
                        <mat-form-field class="mat-block flex-1">
                          <mat-label translate>admin.2fa.verification-message-template</mat-label>
                          <input matInput formControlName="smsVerificationMessageTemplate" required>
                          <mat-error *ngIf="provider.get('smsVerificationMessageTemplate').hasError('required')">
                            {{ "admin.2fa.verification-message-template-required" | translate }}
                          </mat-error>
                          <mat-error *ngIf="provider.get('smsVerificationMessageTemplate').hasError('pattern')">
                            {{ "admin.2fa.verification-message-template-pattern" | translate }}
                          </mat-error>
                        </mat-form-field>

                        <mat-form-field class="mat-block flex-1">
                          <mat-label translate>admin.2fa.verification-code-lifetime</mat-label>
                          <input matInput formControlName="verificationCodeLifetime" type="number" step="1" min="1" required>
                          <mat-error *ngIf="provider.get('verificationCodeLifetime').hasError('required')">
                            {{ "admin.2fa.verification-code-lifetime-required" | translate }}
                          </mat-error>
                          <mat-error *ngIf="provider.get('verificationCodeLifetime').hasError('min') ||
                                            provider.get('verificationCodeLifetime').hasError('pattern')">
                            {{ "admin.2fa.verification-code-lifetime-pattern" | translate }}
                          </mat-error>
                        </mat-form-field>
                      </div>
                      <div *ngSwitchCase="twoFactorAuthProviderType.EMAIL">
                        <mat-form-field class="mat-block flex-1">
                          <mat-label translate>admin.2fa.verification-code-lifetime</mat-label>
                          <input matInput formControlName="verificationCodeLifetime" type="number" step="1" min="1" required>
                          <mat-error *ngIf="provider.get('verificationCodeLifetime').hasError('required')">
                            {{ "admin.2fa.verification-code-lifetime-required" | translate }}
                          </mat-error>
                          <mat-error *ngIf="provider.get('verificationCodeLifetime').hasError('min') ||
                                            provider.get('verificationCodeLifetime').hasError('pattern')">
                            {{ "admin.2fa.verification-code-lifetime-pattern" | translate }}
                          </mat-error>
                        </mat-form-field>
                      </div>
                      <div *ngSwitchCase="twoFactorAuthProviderType.BACKUP_CODE">
                        <mat-form-field class="mat-block flex-1">
                          <mat-label translate>admin.2fa.number-of-codes</mat-label>
                          <input matInput formControlName="codesQuantity" type="number" step="1" min="1" required>
                          <mat-error *ngIf="provider.get('codesQuantity').hasError('required')">
                            {{ "admin.2fa.number-of-codes-required" | translate }}
                          </mat-error>
                          <mat-error *ngIf="provider.get('codesQuantity').hasError('min') ||
                                            provider.get('codesQuantity').hasError('pattern')">
                            {{ "admin.2fa.number-of-codes-pattern" | translate }}
                          </mat-error>
                        </mat-form-field>
                      </div>
                    </ng-container>
                  </ng-template>
                </mat-expansion-panel>
                <mat-divider *ngIf="!$last"></mat-divider>
              </ng-container>
            </fieldset>
            <fieldset class="fields-group">
              <legend class="group-title" translate>admin.2fa.verification-limitations</legend>
              <div class="input-row flex flex-row xs:flex-col gt-xs:gap-2">
                <mat-form-field class="mat-block flex-1">
                  <mat-label translate>admin.2fa.total-allowed-time-for-verification</mat-label>
                  <input matInput required formControlName="totalAllowedTimeForVerification" type="number" step="1" min="60">
                  <mat-error *ngIf="twoFaFormGroup.get('totalAllowedTimeForVerification').hasError('required')">
                    {{ 'admin.2fa.total-allowed-time-for-verification-required' | translate }}
                  </mat-error>
                  <mat-error *ngIf="twoFaFormGroup.get('totalAllowedTimeForVerification').hasError('pattern')
                    || twoFaFormGroup.get('totalAllowedTimeForVerification').hasError('min')">
                    {{ 'admin.2fa.total-allowed-time-for-verification-pattern' | translate }}
                  </mat-error>
                </mat-form-field>
                <mat-form-field class="mat-block flex-1">
                  <mat-label translate>admin.2fa.retry-verification-code-period</mat-label>
                  <input matInput required formControlName="minVerificationCodeSendPeriod" type="number" step="1" min="5">
                  <mat-error *ngIf="twoFaFormGroup.get('minVerificationCodeSendPeriod').hasError('required')">
                    {{ 'admin.2fa.retry-verification-code-period-required' | translate }}
                  </mat-error>
                  <mat-error *ngIf="twoFaFormGroup.get('minVerificationCodeSendPeriod').hasError('pattern')
                    || twoFaFormGroup.get('minVerificationCodeSendPeriod').hasError('min')">
                    {{ 'admin.2fa.retry-verification-code-period-pattern' | translate }}
                  </mat-error>
                </mat-form-field>
                <mat-form-field class="mat-block flex-1">
                  <mat-label translate>admin.2fa.max-verification-failures-before-user-lockout</mat-label>
                  <input matInput formControlName="maxVerificationFailuresBeforeUserLockout" type="number" step="1" min="0" max="65535">
                  <mat-error *ngIf="twoFaFormGroup.get('maxVerificationFailuresBeforeUserLockout').hasError('pattern')
                    || twoFaFormGroup.get('maxVerificationFailuresBeforeUserLockout').hasError('min')
                    || twoFaFormGroup.get('maxVerificationFailuresBeforeUserLockout').hasError('max')">
                    {{ 'admin.2fa.max-verification-failures-before-user-lockout-pattern' | translate }}
                  </mat-error>
                </mat-form-field>
              </div>

              <mat-expansion-panel class="provider">
                <mat-expansion-panel-header>
                  <mat-panel-title class="flex items-center justify-start">
                    <mat-slide-toggle (mousedown)="toggleExtensionPanel($event, providersForm.length, twoFaFormGroup.get('verificationCodeCheckRateLimitEnable').value)"
                                      formControlName="verificationCodeCheckRateLimitEnable">
                    </mat-slide-toggle>
                    {{ 'admin.2fa.verification-code-check-rate-limit' | translate }}
                  </mat-panel-title>
                </mat-expansion-panel-header>
                <ng-template matExpansionPanelContent>
                  <div class="flex flex-row xs:flex-col gt-xs:gap-2">
                    <mat-form-field class="mat-block flex-1">
                      <mat-label translate>admin.2fa.number-of-checking-attempts</mat-label>
                      <input matInput formControlName="verificationCodeCheckRateLimitNumber" required type="number" step="1" min="1">
                      <mat-error *ngIf="twoFaFormGroup.get('verificationCodeCheckRateLimitNumber').hasError('required')">
                        {{ 'admin.2fa.number-of-checking-attempts-required' | translate }}
                      </mat-error>
                      <mat-error *ngIf="twoFaFormGroup.get('verificationCodeCheckRateLimitNumber').hasError('pattern')
                    || twoFaFormGroup.get('verificationCodeCheckRateLimitNumber').hasError('min')">
                        {{ 'admin.2fa.number-of-checking-attempts-pattern' | translate }}
                      </mat-error>
                    </mat-form-field>
                    <mat-form-field class="mat-block flex-1">
                      <mat-label translate>admin.2fa.within-time</mat-label>
                      <input matInput formControlName="verificationCodeCheckRateLimitTime" required type="number" step="1" min="1">
                      <mat-error *ngIf="twoFaFormGroup.get('verificationCodeCheckRateLimitTime').hasError('required')">
                        {{ 'admin.2fa.within-time-required' | translate }}
                      </mat-error>
                      <mat-error *ngIf="twoFaFormGroup.get('verificationCodeCheckRateLimitTime').hasError('pattern')
                    || twoFaFormGroup.get('verificationCodeCheckRateLimitTime').hasError('min')">
                        {{ 'admin.2fa.within-time-pattern' | translate }}
                      </mat-error>
                    </mat-form-field>
                  </div>
                </ng-template>
              </mat-expansion-panel>
            </fieldset>
          </div>
          <div class="flex flex-row items-center justify-end gap-2">
            <button mat-button mat-raised-button color="primary"
                    [disabled]="(isLoading$ | async) || twoFaFormGroup.invalid || !twoFaFormGroup.dirty"
                    type="submit">
              {{'action.save' | translate}}
            </button>
          </div>
        </fieldset>
      </form>
    </mat-card-content>
  </mat-card>
</div>
